﻿using System;
using System.Collections.Generic;
using System.Text;
using DCRF.Common.Interface;
using System.Windows.Forms;
using DCRF.Common.Connection;
using AdminConsole.TreeNodes.Nodes.Single;
using DCRF.Common.Support;

namespace AdminConsole.TreeNodes.Nodes.Collection
{
    public class EndPointsNode : BaseNode
    {
        public IBlockWeb myWeb = null;
        public Guid myId = Guid.Empty;
        public string myConnectorKey = null;

        public EndPointsNode(IBlockWeb web, Guid id, string key)
        {
            myWeb = web;
            myId = id;
            myConnectorKey = key;            
        }

        public override void Attach(TreeNode myNode)
        {
            myNode.Tag = this;
            myNode.SelectedImageKey = myNode.ImageKey = "EndPoints";
            myNode.Text = "EndPoints";

            addTempNode(myNode);
        }

        protected override void buildChildrenList()
        {
            children.Clear();

            //we are just allowed to get a list of endpoints description
            List<string> endpoints = null;

            if (myId == Guid.Empty)
            {
                endpoints = myWeb.GetBlockWebMetaInfo(BlockWebMetaInfoType.GlobalConnectorEndpoints, myConnectorKey) as List<string>;
            }
            else
            {
                endpoints = (List<string>)myWeb[myId].ProcessRequest("ProcessMetaInfo", BlockMetaInfoType.ConnectorEndpoints, myConnectorKey, null);
            }

            foreach (string ep in endpoints)
            {
                int idx = ep.IndexOf("@");
                string key = ep.Substring(0, idx);
                string desc = ep.Substring(idx + 1);

                children.Add(new EndPointNode(myWeb, myId, myConnectorKey, desc, key));
            }
        }

        public override List<string> GetCommands(TreeNode myNode)
        {
            return new List<string>() { "ATT", "REF" };
        }

        public override void ExecuteCommand(string cmdKey, TreeNode myNode)
        {
            if (cmdKey == "REF")
            {
                refresh(myNode);
            }
            else if (cmdKey == "ATT")
            {
                doAttachEndPoint(myNode);
            }
        }

        public void doAttachEndPoint(TreeNode myNode)
        {
            doAttachEndPoint(myNode, Guid.Empty, null, null);
        }

        public void doAttachEndPoint(TreeNode myNode, Guid blockId, string service, string connector)
        {
            //attach endpoint
            frmAttachEndPoint ep = new frmAttachEndPoint();

            if (blockId != Guid.Empty) ep.BlockId = blockId;
            if (service != null)
            {
                ep.Service = service;
                ep.IsValue = false;
            }

            if (connector != null)
            {
                ep.ChainConnectorKey = connector;
                ep.IsValue = false;
            }

            ep.Text = "Attach EndPoint for " + myConnectorKey;

            if (ep.ShowDialog() == DialogResult.OK)
            {
                string epKEy = null;
                string desc = "";

                if (ep.IsValue)
                {
                    epKEy = myWeb[myId][myConnectorKey].AttachEndPoint(ep.Value);
                    desc = ep.Value.ToString();
                }
                else
                {
                    if (ep.ChainConnectorKey == "")
                    {
                        epKEy = myWeb[myId][myConnectorKey].AttachEndPoint(ep.BlockId, ep.Service, ep.PredefinedValues);
                        desc = ep.BlockId.ToString() + " : " + ep.Service;
                    }
                    else
                    {
                        epKEy = myWeb[myId][myConnectorKey].ChainEndPoint(ep.BlockId, ep.ChainConnectorKey);

                        if (ep.BlockId != Guid.Empty)
                        {
                            desc = "Connector: " + ep.ChainConnectorKey + " @ " + ep.BlockId.ToString();
                        }
                        else
                        {
                            desc = "GlobalConnector: " + ep.ChainConnectorKey;
                        }
                    }
                }

                children.Add(new EndPointNode(myWeb, myId, myConnectorKey, desc, epKEy));

                //refresh but do not re-connect and re-read endpoints, just update UI
                refresh(myNode, false);
            }
        }
    }
}

